package cn.smart.service.impl;

import cn.smart.mapper.FgroupMapper;
import cn.smart.mapper.GardenMapper;
import cn.smart.mapper.UserMapper;
import cn.smart.pojo.Fgroup;
import cn.smart.pojo.Garden;
import cn.smart.pojo.User;
import cn.smart.service.FtpService;
import cn.smart.util.ExceptionUtil;
import cn.smart.util.FtpUtil;
import cn.smart.util.Info;
import cn.smart.util.SmartResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author Xiezh
 * @Description:
 * @create 2017/8/15
 */
@Service
public class FtpServiceImpl implements FtpService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GardenMapper gardenMapper;
    @Autowired
    private FgroupMapper fgroupMapper;
    @Value("${FTP_ADDRESS}")
    private String ftpAddress;//ftp地址
    @Value("${FTP_PORT}")
    private int ftpPort;//ftp端口号
    @Value("${FTP_USERNAME}")
    private String ftpUserName;//ftp账号
    @Value("${FTP_PASSWORD}")
    private String ftpPassword;//ftp密码
    @Value("${FTP_BASE_PATH}")
    private String ftpBasePath;//照片在ftp服务器上保存的位置
    @Value("${IMAGE_BATH_URL}")
    private String imageBathUrl;//图片服务器目录

    /**
     * 文件上传
     *
     * @param file
     * @param type 上传的位置 1=用户头像 2=花园 3=花
     * @return
     */
    @Override
    public SmartResult uploadFile(MultipartFile file, int type, int id) throws Exception {
        //获取文件后缀
        String filename = file.getOriginalFilename();
        String[] split = filename.split("\\.");
        if (split.length == 0) {
            return new SmartResult(200, Info.FAIL, "文件格式错误");
        }
        String prex = split[split.length - 1];
        //创建新的文件名
        String saveFile = UUID.randomUUID() + "." + prex;
        //判断上传接口类型
        String fileDir = "";
        Object obj = null;
        String oldFilePath = "";
        if (type == 1) {
            //用户头像
            fileDir = "user";
            User user = userMapper.selectByPrimaryKey(id);
            if (user == null) {
                return new SmartResult(200, Info.NOT_EXIST, "用户不存在");
            }
            oldFilePath = user.getUpic();
            user.setUpic(imageBathUrl + "/" + fileDir + "/" + saveFile);
            int i = userMapper.updateByPrimaryKey(user);
        } else if (type == 2) {
            //花园图片
            fileDir = "garden";
            Garden garden = gardenMapper.selectByPrimaryKey(id);
            if (garden == null) {
                return new SmartResult(200, Info.NOT_EXIST, "花园不存在");
            }
            oldFilePath = garden.getGpic();
            garden.setGpic(imageBathUrl + "/" + fileDir + "/" + saveFile);
            gardenMapper.updateByPrimaryKey(garden);
        } else if (type == 3) {
            //花路的图片
            fileDir = "flower";
            Fgroup fgroup = fgroupMapper.selectByPrimaryKey(id);
            if (fgroup == null) {
                return new SmartResult(200, Info.NOT_EXIST, "花路不存在");
            }
            oldFilePath = fgroup.getFgpic();
            fgroup.setFgpic(imageBathUrl + "/" + fileDir + "/" + saveFile);
            fgroupMapper.updateByPrimaryKey(fgroup);

        } else {
            return new SmartResult(200, Info.FAIL, "未知的文件用途");
        }
        //图片上传
        FtpUtil.uploadFile(ftpAddress, ftpPort, ftpUserName, ftpPassword, ftpBasePath, fileDir, saveFile, file.getInputStream());
        if (oldFilePath != null && !StringUtils.isEmpty(oldFilePath)) {
            String[] deleteSplit = oldFilePath.split("\\/");
            if (deleteSplit.length > 0) {
                FtpUtil.deleteFile(ftpAddress, ftpPort, ftpUserName, ftpPassword, ftpBasePath, fileDir, deleteSplit[deleteSplit.length-1]);
            }

        }


        return new SmartResult(200, Info.SUCCESS, null);
    }
}
